<!-- 定义模版对象 -->
<template>
  <div class="login_container">
    <div class="login_box">
      <!-- 头像区域-->
    <div class="avatar_box">
    </div>

      <!-- 登陆表单区域 ref代表当前表单引用对象-->
      <el-form :model="loginForm" :rules="rules" ref="loginFormRef" label-width="0" class="login_form">
        <el-form-item prop="username">
          <el-input prefix-icon="iconfont iconuser" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input prefix-icon="iconfont iconsuo" type="password" v-model="loginForm.password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetBtn">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<!-- 定义JS变量 -->
<script>
/*eslint-disable*/
  export default {
    data() {
      return {
        loginForm: {
          username: 'admin',
          password: 'admin123456',
          image:""
        },
        rules: {
          username: [{
              required: true,
              message: "用户名为必填项",
              trigger: "blur"
            },
            {
              min: 3,
              max: 20,
              message: "长度应该为3-20",
              trigger: "blur"
            }
          ],
          password: [{
              required: true,
              message: "密码为必填项",
              trigger: "blur"
            },
            {
              min: 3,
              max: 20,
              message: "长度应该为3-20",
              trigger: "blur"
            }
          ]

        }

      }
    },
    methods: {
      resetBtn() {
        this.$refs.loginFormRef.resetFields()
      },
      login() {
        this.$refs.loginFormRef.validate(async (bool, o) => {
          if (bool) {
            var {
              data: result
            } = await this.$http.post('/user/login', this.loginForm)
            console.log(result)
            if (result.status !== 200) {
              this.$message.error("您输错了用户名或者密码，wocao")
            }
            if(result.status===200){
              this.$message({
                message: "您成功了臭吊毛",
                type: 'success'
              })
              window.sessionStorage.setItem("token",result.data);
              this.$router.push("/home")
            }

          } else {
            var a = '';
            if (o.username)
              a += (o.username[0].message)
            if (o.password)
              a += (o.password[0].message)
            if (!a == '')
              this.$message(a)
          }
        })
      }
    }

  }
</script>

<!-- 防止组件冲突 -->
<style lang="less" scoped>
.login_container{
  background-color: #2b4b6b;
  height: 100%;
}

.login_box {
  width: 450px;
  height: 300px;
  background-color: #FFFFFF;
  /* 设定圆弧角*/
  border-radius: 10px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);

  .avatar_box {
    height: 130px;
    width: 130px;
    border: 1px solid #EEEEEE;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #DDDDDD; /* 添加阴影*/
    position: absolute;           /* 绝对定位*/
    left: 50%;                    /* 距离左侧50%*/
    transform: translate(-50%,-50%);   /*回调50%*/
    background-color: #FFFFFF;
    img {
      height: 100%;
      width: 100%;
      border-radius: 50%;
      background-color: #EEEEEE;
    }
  }

  .btns {
    display: flex;
    justify-content: flex-end;

  }

  .login_form {
    position: absolute;
    bottom: 0;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>
